﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaopxFramework.Common.Method;
using BaopxFramework.DAL;

namespace BaopxFramework.Admin.ManageTaxonomy
{
    public partial class ManageTaxonomyName : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        private void LoadData()
        {
            using (var d = new Repository())
            {
                grd.DataSource = d.GetTaxonomyNames();
                grd.DataBind();
            }
        }

        protected void grd_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (var d = new Repository())
                    {
                        var txtName = (TextBox) e.Item.FindControl("txtName");
                        var chkActive = (CheckBox) e.Item.FindControl("chkActive");
                        var chkIsUseForPost = (CheckBox) e.Item.FindControl("chkIsUseForPost");
                        var chkIsUseForProduct = (CheckBox) e.Item.FindControl("chkIsUseForProduct");
                        d.InsertTaxonomyName(txtName.Text.Standard(), chkActive.Checked, chkIsUseForPost.Checked,
                                             chkIsUseForProduct.Checked);
                    }
                    break;
                case "cmdUpdate":
                    using (var d = new Repository())
                    {
                        var txtName = (TextBox) e.Item.FindControl("txtName");
                        var chkActive = (CheckBox) e.Item.FindControl("chkActive");
                        var chkIsUseForPost = (CheckBox) e.Item.FindControl("chkIsUseForPost");
                        var chkIsUseForProduct = (CheckBox) e.Item.FindControl("chkIsUseForProduct");
                        d.UpdateTaxonomyName(e.CommandArgument.ToInt(), txtName.Text.Standard(), chkActive.Checked,
                                             chkIsUseForPost.Checked,
                                             chkIsUseForProduct.Checked);
                    }
                    break;
                case "cmdDelete":
                    using (var d = new Repository())
                    {
                        d.DeleteTaxonomyName(e.CommandArgument.ToInt());
                    }
                    break;
            }
            LoadData();
        }
    }
}